package com.example.myxm.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.myxm.App;
import com.example.myxm.R;
import com.example.myxm.auth.LoginManager;
import com.example.myxm.databinding.ActivitySysSettingBinding;
import com.example.myxm.module.ResultBean;
import com.example.myxm.module.UserBean;

public class SysSettingActivity extends BaseActivity<ActivitySysSettingBinding> {

    @NonNull
    @Override
    protected ActivitySysSettingBinding getViewBinding(@NonNull LayoutInflater inflater) {
        return ActivitySysSettingBinding.inflate(inflater);
    }


    @Override
    protected void initData() {
        ResultBean<UserBean> userIdBean = App.getDbHelperStatic().getUserIdBean(LoginManager.getCurrentUserId(this));
        getBinding().toolbar.setNavigationIcon(R.drawable.ic_back);
        getBinding().toolbar.setTitle("当前用户 : " + userIdBean.getData().getUsername());
        getBinding().toolbar.setNavigationOnClickListener(v -> finish());



        getBinding().btModifySys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBinding().btModifySys.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        String oldPasswordInput = getBinding().etYpwdSys.getText().toString().trim();

                        String newPasswordInput = getBinding().etXpwdSys.getText().toString().trim();

                        String confirmPasswordInput = getBinding().etZxpwdSys.getText().toString().trim();

                        if (oldPasswordInput.isEmpty() ||
                                newPasswordInput.isEmpty() ||
                                confirmPasswordInput.isEmpty()) {
                            showToast("密码不能为空");
                            return;
                        }

                        if (!newPasswordInput.equals(confirmPasswordInput)) {
                            showToast("两次输入的新密码不一致");
                            return;
                        }

                        if (userIdBean == null || userIdBean.getCode() != 200) {
                            showToast("无法获取用户信息，请稍后重试");
                            return;
                        }

                        String storedPassword = userIdBean.getData().getPassword();
                        if (!oldPasswordInput.equals(storedPassword)) {
                            showToast("原始密码错误");
                            return;
                        }

                        ResultBean<Integer> integerResultBean = App.getDbHelperStatic().updatePassword(
                                LoginManager.getCurrentUserId(SysSettingActivity.this),
                                userIdBean.getData().getUsername(),
                                newPasswordInput
                        );

                        if (integerResultBean.getCode() == 200) {
                            showToast("密码修改成功");
                            LoginManager.logout(SysSettingActivity.this);
                            Intent intent = new Intent(SysSettingActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            showToast("密码修改失败，请稍后重试");
                        }
                    }
                });


            }
        });

        getBinding().btTuichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginManager.logout(SysSettingActivity.this);
                Intent intent = new Intent(SysSettingActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
    private void showToast(String str){
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }
}